package jtk.project4.fleet.screen.invoice;

import java.awt.FlowLayout;
import java.util.List;

import jtk.project4.fleet.field.InvoiceDetailField;
import jtk.project4.fleet.action.CancelInvoiceAction;
import jtk.project4.fleet.action.SaveInvoice;
import nl.coderight.jazz.action.Action;
import nl.coderight.jazz.form.FormField;
import nl.coderight.jazz.form.FormLayout;
import nl.coderight.jazz.form.FormView;
import nl.coderight.jazz.form.control.GroupControl;
import nl.coderight.jazz.form.control.TabControl;
import nl.coderight.jazz.form.control.TabItem;
import nl.coderight.jazz.form.field.button.PushButton;
import nl.coderight.jazz.form.validation.ValidationError;
import nl.coderight.jazz.form.validation.ValidationEvent;
import jtk.project4.fleet.event.InvoiceEvent;

public class InvoiceView extends FormView{
	private TabControl InvoiceTab;
	private TabItem InvoiceDetailTab;
	private TabItem MiscItemTab;
	private TabItem paymentTab;

	private PushButton submitButton;
	private PushButton cancelButton;
	private Action submitAction;
	private Action cancelAction;
	
	
	public void handleEvent(InvoiceEvent evt){
		switch (evt.getType()){
		case CANCEL:
			close();
			break;
		case SUBMIT:
			List<ValidationError> errors = validate();
			if (!errors.isEmpty()) {
				errors.get(0).getField().focus();
				break;
			} else {
				submit();
				close();
			}
		default:
			propagateEvent(evt);
			break;
		}
	}
	
	@Override
	protected void init() {
		// TODO Auto-generated method stub
		createTabFields();		
		createFields();
		createLayout();
	}
	
	private void createLayout() {
		addField(createForm());
	}
	
	private void createFields() {
		submitButton = new PushButton(new SaveInvoice());
		cancelButton = new PushButton(new CancelInvoiceAction());
	}
	
	private void createTabFields()
	{
		InvoiceTab = new TabControl();
		InvoiceDetailTab = new TabItem("Invoice Detail");
		MiscItemTab = new TabItem("Misc. Items");
		paymentTab = new TabItem("Payments Received");
	}
	
	private FormField createTabs()
	{
		TabControl form = InvoiceTab;
		form.addTab(InvoiceDetailTab, createGeneralTab());
		form.addTab(MiscItemTab, createGeneralTab());
		form.addTab(paymentTab, createGeneralTab());
		return form;
	}
	
	private GroupControl createGeneralTab() {
		GroupControl form = new GroupControl();
		form.setLayout(new FormLayout())
		.addField(new InvoiceDetailField("InvDetail"))
		.addRow()
		;				
		
		return form;
	}
	
	private FormField createForm()
	{
		GroupControl form = new GroupControl();
		
		form.addField(new InvoiceDetailField("InvDetail"),NORTH);
		form.addField(createButtons(), SOUTH);
		
		return form;
	}
	
	private GroupControl createGeneralForm() {
		GroupControl form = new GroupControl();
		form.addField(createGeneralForm(), NORTH);
		form.addField(createButtons(), SOUTH);
		return form;
	}
	
	private GroupControl createButtons() {
		GroupControl buttons = new GroupControl();
		buttons.setLayout(new FlowLayout(FlowLayout.RIGHT));
		buttons.addField(submitButton);
		buttons.addField(cancelButton);
		return buttons;
	}
}
